//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Services;
using NopSolutions.NopCommerce.BusinessLogic.Utils;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class ProductPrice2Control : BaseNopFrontendUserControl
    {
        private IProductService _productService;
        private ITaxService _taxService;
        private ICurrencyService _currencyService;
        private ISettingService _settingService;

        private int _productId;

        public ProductPrice2Control()
        {
            _productService = IoC.Resolve<IProductService>();
            _taxService = IoC.Resolve<ITaxService>();
            _currencyService = IoC.Resolve<ICurrencyService>();
            _settingService = IoC.Resolve<ISettingService>();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            Load += new EventHandler(ProductPrice2Control_Load);
            //PreRender += new EventHandler(ProductPrice2Control_PreRender);
        }

        void ProductPrice2Control_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();

        }

        //void ProductPrice2Control_PreRender(object sender, EventArgs e) {
        //    if (!IsPostBack)
        //        BindData();
        //}

        private void BindData()
        {
            var product = _productService.GetProductById(this.ProductId);
            if (product != null) {
                var productVariantCollection = product.ProductVariants;
                if (productVariantCollection.Count > 0) {
                    if (!product.HasMultipleVariants) {
                        var productVariant = productVariantCollection[0];
                        if (!_settingService.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                            (NopContext.Current.User != null &&
                            !NopContext.Current.User.IsGuest)) {

                            // no price, no show
                            if (productVariant.Price == 0) {
                                this.Visible = false;
                            }

                            if (productVariant.CustomerEntersPrice) {
                                lblOldPrice.Visible = false;
                                lblPrice.Visible = false;
                            } else {
                                if (productVariant.CallForPrice) {
                                    lblOldPrice.Visible = false;
                                    lblPrice.Text = GetLocaleResourceString("Products.CallForPrice");
                                } else {
                                    decimal taxRate = decimal.Zero;
                                    decimal oldPriceBase = _taxService.GetPrice(productVariant, productVariant.OldPrice, out taxRate);
                                    decimal finalPriceBase = _taxService.GetPrice(productVariant, PriceHelper.GetFinalPrice(productVariant, true), out taxRate);

                                    decimal oldPrice = _currencyService.ConvertCurrency(oldPriceBase, _currencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                    decimal finalPrice = _currencyService.ConvertCurrency(finalPriceBase, _currencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);

                                    if (finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero) {
                                        lblOldPrice.Text = PriceHelper.FormatPrice(oldPrice);
                                    } else {
                                        lblOldPrice.Visible = false;
                                    }

                                    lblPrice.Text = PriceHelper.FormatPrice(finalPrice);

                                    if (product.ForceDisplayWOVat) {
                                        decimal reallyFinalPrice = finalPrice - ((finalPrice * taxRate) / (100 + taxRate));
                                        lblPrice.Text = PriceHelper.FormatPrice(reallyFinalPrice, false, NopContext.Current.WorkingCurrency, NopContext.Current.WorkingLanguage, false);
                                    }
                                }
                            }
                        } else {
                            this.Visible = false;
                        }
                    } else {
                        var productVariant = product.MinimalPriceProductVariant;
                        if (productVariant != null) {
                            if (!_settingService.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                                (NopContext.Current.User != null &&
                                !NopContext.Current.User.IsGuest)) {
                                if (productVariant.CustomerEntersPrice) {
                                    lblOldPrice.Visible = false;
                                    lblPrice.Visible = false;
                                } else {
                                    if (productVariant.CallForPrice) {
                                        lblOldPrice.Visible = false;
                                        lblPrice.Text = GetLocaleResourceString("Products.CallForPrice");
                                    } else {
                                        decimal taxRate = decimal.Zero;
                                        decimal fromPriceBase = _taxService.GetPrice(productVariant, PriceHelper.GetFinalPrice(productVariant, false), out taxRate);
                                        decimal fromPrice = _currencyService.ConvertCurrency(fromPriceBase, _currencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                        if (product.ForceDisplayWOVat) {
                                            fromPrice = fromPrice - ((fromPrice * taxRate) / (100 + taxRate));
                                        }

                                        lblOldPrice.Visible = false;
                                        lblPrice.Text = String.Format(GetLocaleResourceString("Products.PriceRangeFromText"), PriceHelper.FormatPrice(fromPrice));
                                    }
                                }
                            } else {
                                lblOldPrice.Visible = false;
                                lblPrice.Visible = false;
                            }
                        }
                    }
                } else {
                    lblOldPrice.Visible = false;
                    lblPrice.Visible = false;
                }
            }
        }

        public int ProductId
        {
            get
            {
                return _productId;
            }
            set
            {
                _productId = value;
            }
        }
    }
}